;;This file is for functions likely to be found useful by those connecting to lensfield when it is running a swank server
;;--john

;;start repl by calling mvn clojure:repl (remember to be in sub-directory, export environment variable)
;;on connecting to the lensfield repl, no namespaces are loaded

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;type at REPL
(comment 
  (do  (in-ns 'user)  (load-file "../scr.clj")  (in-ns 'lensfield.template-match)  (user/stuff))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;Alternatively
;;find this file in emacs, and compile it using C-c C-k. It should side-effect by printing out 
;;the classpath and namespaces which are currently visible at the repl.

;;It will also define several functions that I have found useful.

;;In particular, (stuff) fires off a random bag of things that I like doing.

(defn get-classpath []
  (seq (.getURLs (java.lang.ClassLoader/getSystemClassLoader))))

(defn all-non-swank-namespaces []
  (for [s (for [n (all-ns)] (name (ns-name n))) :when (not (. s contains "swank"))] s))

(defn all-lensfield-namespaces []
  (for [s (for [n (all-ns)] (name (ns-name n))) :when (. s contains "lensfield")] s))

(defn banner [& s]
  (println "-----------------------------------------------------------")  
  (apply println s)
  (println "-----------------------------------------------------------"))

(defn get-public-symbols [ns] (map first (ns-publics ns)))
(defn get-all-symbols    [ns] (map first (ns-interns ns)))
(defn get-non-test-public-symbols [ns] (filter #(not (. (name %) startsWith "test-")) (map first (ns-publics ns))))

(defn print-current-environment []
 ;(banner "The classpath:")
 ;(println (get-classpath))
  (banner "all the namespaces which do not have swank (the emacs interface) in the name.")
  (println (all-non-swank-namespaces))
  (banner "all the namespaces which have lensfield in the name.")
  (println (all-lensfield-namespaces))
  (banner (str "current namespace: [ " *ns* " ]"))
  (println "public symbols")
  (println (get-public-symbols *ns*))
  (println "all symbols")
  (println (get-all-symbols *ns*))
  (println "at this point, you probably want to type: (do(in-ns 'lensfield.template-match)(user/stuff))")
  (println "(user/stuff) will load all the code and some repl utilities, as well as running all the unit tests"))

;;sanity check to tell what we have loaded in an uncontaminated REPL 
(print-current-environment)
;;although, of course, we've already contaminated it with the functions above

;;now we can start contaminating it for real:

(defn use-things []
  (use 'clojure.test)
  (use 'clojure.contrib.pprint)
  (use 'clojure.contrib.repl-utils))

(require 'lensfield.directory_walker)
(require 'clojure.contrib.pprint)

(defn find-all-lensfield-files[]
  (let [files (lensfield.directory-walker/walk-dir "./src/main/clj" :extension "clj")]
    (map #(. % getCanonicalPath) files)))

(defn load-all-lensfield-files []
  (let [lfs (find-all-lensfield-files)]
    (doall (map load-file lfs))))

(defn list-public-and-private[ns]
  (println "public symbols that do not begin with test-")
  (clojure.contrib.pprint/pprint (get-non-test-public-symbols ns))
  (println "public symbols")
  (clojure.contrib.pprint/pprint (get-public-symbols ns))
  (println "all interns")
  (println (get-all-symbols ns)))

(require 'clojure.test)

(defn stuff[]
  (banner "loading favourite repl libraries")
  (use-things)
  (banner "loading all lensfield files...")
  (load-all-lensfield-files)
  (println "done")
  (banner "all the namespaces which do not have swank (the emacs interface) in the name.")
  (println (all-non-swank-namespaces))
  (banner "all the namespaces which have lensfield in the name")
  (println (all-lensfield-namespaces))
  (banner "running tests")
  (clojure.test/run-all-tests)
  (banner "defined in the user namespace:")
  (list-public-and-private 'user)
  (banner "defined in the current namespace:" *ns*)
  (list-public-and-private *ns*)

  (println "

; Things to note:

;the gaussian short example is broken because webapp takes two arguments
;you always need that damned environment variable.



; Current tasks are :

; Unit testing template match
; Writing some sort of manual
; Whole system tests on a clean UNIX box 

; things to type at the repl (or use C-x C-r):


; (do(in-ns 'lensfield.template-match)(user/stuff))

; (user/list-public-and-private *ns*)
; remember the clojure.contrib.pp macro for pretty-printing the last thing output
; (run-all-tests)
; (run-tests)
; (lensfield.core/lf-build)
")


  )


; From a repl created with mvn clojure:repl or mvn clojure:swank
; you may load this file and run (stuff) by typing:
; (do (load-file "../scr.clj")(user/stuff))








